package yt.demo.appcommonbean.http;

import jakarta.servlet.http.HttpServletRequest;


public class Request {
    // 获取客户端 IP 地址
    public static String getClientIp(HttpServletRequest request) {
        String clientIp = request.getHeader("X-Forwarded-For");
        if (clientIp == null || clientIp.isEmpty()) {
            clientIp = request.getRemoteAddr();
        } else {
            clientIp = clientIp.split(",")[0];
        }
        if (clientIp.equals("0:0:0:0:0:0:0:1")) {
            clientIp = "127.0.0.1";  // 转换为本地IP
        }
        return clientIp;
    }

}
